var mongoose = require('mongoose');
var config   = require('../config');
var logger = require('../common/logger')
var mongodb = config.mongo;
let env = process.env.NODE_ENV || 'test';
if (env === 'test') {
  mongoose.connect(`${mongodb.uri}`, function (err) {
    if (err) {
      logger.error('connect to %s error: ', mongodb, err.message)
      process.exit(1)
    }
  });
}else{
  mongoose.connect(`mongodb://${mongodb.options.user}:${mongodb.options.pass}@localhost:${mongodb.port}/2hours?authSource=admin`, function (err) {
    if (err) {
      logger.error('connect to %s error: ', mongodb, err.message)
      process.exit(1)
    }
  });
}


// mongoose.connect(config.mongo, {
//   server: {poolSize: 20}
// }, function (err) {
//   if (err) {
//     logger.error('connect to %s error: ', config.db, err.message);
//     process.exit(1);
//   }
// });

// models
require('./user');
require('./topic');
require('./reply');
require('./topic_collect');
require('./message');

exports.User         = mongoose.model('User');
exports.Topic        = mongoose.model('Topic');
exports.Reply        = mongoose.model('Reply');
exports.TopicCollect = mongoose.model('TopicCollect');
exports.Message      = mongoose.model('Message');
